package org.spring.blog.service.impl;

import org.spring.blog.dao.FriendLinkDao;
import org.spring.blog.pojo.FriendLink;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.IFriendLinkService;
import org.spring.blog.utils.LRConstants;
import org.spring.blog.utils.SnowflakeIdWorker;
import org.spring.blog.utils.TextUtil;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class FriendLinkService implements IFriendLinkService {

    final
    FriendLinkDao friendLinkDao;

    final
    SnowflakeIdWorker snowflakeIdWorker;

    public FriendLinkService(SnowflakeIdWorker snowflakeIdWorker, FriendLinkDao friendLinkDao) {
        this.snowflakeIdWorker = snowflakeIdWorker;
        this.friendLinkDao = friendLinkDao;
    }

    /**
     * 获取友情链接列表
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult list() {
        // check data
        // complete date
        Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");

        List<FriendLink> all = friendLinkDao.findAll(new Specification<FriendLink>() {
            private static final long serialVersionUID = -8226310154308748018L;

            @Override
            public Predicate toPredicate(Root<FriendLink> _root, CriteriaQuery<?> _criteriaQuery, CriteriaBuilder _criteriaBuilder) {
                return _criteriaBuilder.equal(_root.get("state").as(String.class), LRConstants.db_publish_status);
            }
        }, sort);

        return ResponseResult.success().setData(all);
    }

    /**
     * 获取友情链接列表
     *
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult listForAdmin() {
        // check data
        // complete date
        Sort sort = new Sort(Sort.Direction.DESC, "createTime", "order");

        List<FriendLink> all = friendLinkDao.findAll(sort);

        return ResponseResult.success().setData(all);
    }

    /**
     * 查询友情链接
     *
     * @param _id 对象id
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult query(String _id) {
        FriendLink oneById = friendLinkDao.findOneById(_id);

        if (oneById == null) {
            return ResponseResult.failed(ResponseState.friendLink_not_have_message, ResponseState.friendLink_not_have_code);
        }
        return ResponseResult.success().setData(oneById);
    }

    /**
     * 添加友情链接
     *
     * @param _friendLink 友情链接对象
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult insertFriend(FriendLink _friendLink) {
        // check data
        if (TextUtil.isEmpty(_friendLink.getUrl())) {
            return ResponseResult.failed(ResponseState.friendLink_url_cant_empty_message, ResponseState.friendLink_url_cant_empty_code);
        }

        if (TextUtil.isEmpty(_friendLink.getLogo())) {
            return ResponseResult.failed(ResponseState.friendLink_logo_cant_empty_message, ResponseState.friendLink_logo_cant_empty_code);
        }

        if (TextUtil.isEmpty(_friendLink.getName())) {
            return ResponseResult.failed(ResponseState.friendLink_name_cant_empty_message, ResponseState.friendLink_name_cant_empty_code);
        }

        // complete data
        _friendLink.setId(String.valueOf(snowflakeIdWorker.nextId()))
                .setOrder(LRConstants.db_default_order_value)
                .setState(LRConstants.db_publish_status)
                .setCreateTime(new Date())
                .setUpdateTime(new Date());
        // save data

        friendLinkDao.save(_friendLink);

        return ResponseResult.success();
    }

    /**
     * 删除友情链接
     *
     * @param _id 链接id
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult delete(String _id) {

        friendLinkDao.deleteById(_id);
        return ResponseResult.success();
    }

    /**
     * 更新友情链接
     *
     * @param _id         链接id
     * @param _friendLink 友情链接对象
     * @return 自定义返回对象
     */
    @Override
    public ResponseResult update(String _id, FriendLink _friendLink) {

        FriendLink oneById = friendLinkDao.findOneById(_id);

        if (oneById == null) {
            return ResponseResult.failed(ResponseState.friendLink_not_have_message, ResponseState.friendLink_not_have_code);
        }

        if (!TextUtil.isEmpty(_friendLink.getLogo())) {
            oneById.setLogo(_friendLink.getLogo());
        }

        if (!TextUtil.isEmpty(_friendLink.getName())) {
            oneById.setName(_friendLink.getName());
        }

        if (!TextUtil.isEmpty(_friendLink.getUrl())) {
            oneById.setUrl(_friendLink.getUrl());
        }

        oneById.setOrder(_friendLink.getOrder())
                .setUpdateTime(new Date())
                .setState(_friendLink.getState());

        friendLinkDao.save(oneById);


        return ResponseResult.success();
    }
}
